﻿Imports System.IO
Imports System.Reflection

Public Module modCommon
	Private lErrCounter As Long

	Public Function PlaySound(ByVal strSoundFilePath As String) As Boolean
		If String.IsNullOrEmpty(strSoundFilePath) Then Return False
		Try
			If System.IO.File.Exists(strSoundFilePath) = False Then Return False
			My.Computer.Audio.Play(strSoundFilePath, AudioPlayMode.Background)
			Return True
		Catch ex As Exception
			RegisterException(ex)
		End Try
		Return False
	End Function

	Public Function PlaySound(ByVal sound As Media.SystemSound) As Boolean
		If sound Is Nothing Then Return False
		Try
			My.Computer.Audio.PlaySystemSound(sound)
			Return True
		Catch ex As Exception
			RegisterException(ex)
		End Try
		Return False
	End Function

	Public Sub RegisterException(ByVal exc As Exception)
		If exc Is Nothing Then Exit Sub
		My.Application.Log.WriteException(exc, TraceEventType.Error, My.Application.Info.ProductName, lErrCounter)
		If lErrCounter + 1 > Long.MaxValue Then lErrCounter = 0
		lErrCounter += 1
	End Sub

	Public Sub ExportErrorLog()
		Try
			Using dlg As New System.Windows.Forms.SaveFileDialog()
				dlg.AddExtension = True
				dlg.AutoUpgradeEnabled = True
				dlg.CheckPathExists = True
				dlg.DefaultExt = Path.GetExtension(My.Application.Log.DefaultFileLogWriter.FullLogFileName)
				dlg.Filter = "All files (*.*)|*,*|Log files (*." & _
	dlg.DefaultExt & ")|*." & dlg.DefaultExt
				dlg.FilterIndex = 2
				dlg.OverwritePrompt = True
				dlg.SupportMultiDottedExtensions = True
				dlg.Title = "Enter file path for log export..."
				If dlg.ShowDialog() = DialogResult.OK Then
					My.Application.Log.DefaultFileLogWriter.Flush()
					File.Copy(My.Application.Log.DefaultFileLogWriter.FullLogFileName, dlg.FileName, True)
				End If
			End Using
		Catch ex As Exception
			MessageBox.Show(ex.Message.Trim(), "Error(s) while saving debug log", _
			 MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try
	End Sub

	Public Sub ShowAboutInfoUI()
		MessageBox.Show(My.Application.Info.Description, "About " + _
		 My.Application.Info.ProductName + " " + My.Application.Info.Version.ToString(4), _
		 MessageBoxButtons.OK, MessageBoxIcon.Information)
	End Sub

	Public Sub BindProperty(ByVal collBindings As ControlBindingsCollection, _
	ByVal strPropName As String, ByVal oSource As Object, ByVal strDataMember As String, _
	Optional ByVal booFormattingEnabled As Boolean = True, _
	Optional ByVal eUpdateMode As DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged)
		If collBindings Is Nothing Or strPropName Is Nothing Or oSource Is Nothing Or _
		strDataMember Is Nothing Then Exit Sub

		Dim b = collBindings.Item(strPropName)
		If b IsNot Nothing Then
			collBindings.Remove(b)
		End If

		collBindings.Add(strPropName, oSource, strDataMember, booFormattingEnabled, eUpdateMode)
	End Sub

	Public Sub AddShortcutToSpecialFolder(ByVal name As String, ByVal appPath As String, ByVal folder As Environment.SpecialFolder)
		Dim destDir = Environment.GetFolderPath(folder)
		Using writer = New StreamWriter(destDir + "\" + name + ".url")

			writer.WriteLine("[InternetShortcut]")
			writer.WriteLine("URL=file://" + appPath)
			writer.WriteLine("IconIndex=0")
			Dim ico = appPath.Replace("\", "/")
			writer.WriteLine("IconFile=" + ico)

			writer.Flush()
			writer.Close()
		End Using
		'File.Move(destDir + "\" + name + ".url", destDir + "\ " + name + " .url")
	End Sub

	Public Function IsFirstRun() As Boolean
		Dim val = My.Computer.Registry.CurrentUser.GetValue("Notificator")
		Return val Is Nothing
	End Function

	Public Function RegisterFirstRun(ByVal booSuggestShortcut As Boolean) As Boolean
		If IsFirstRun() = False Then Return False
		My.Computer.Registry.CurrentUser.SetValue("Notificator", Now)
		If booSuggestShortcut = False Then Return True
		If MessageBox.Show("Congatulations!" & vbCrLf & "This is first run of " & _
		 My.Application.Info.ProductName & "." & vbCrLf & _
		 "Do you want to run this application on windows startup?", _
		 My.Application.Info.ProductName & " " & My.Application.Info.Version.ToString(4), _
		 MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) <> DialogResult.Yes Then Return False
		Dim path = Assembly.GetExecutingAssembly().Location
		AddShortcutToSpecialFolder(My.Application.Info.ProductName, _
		  path, Environment.SpecialFolder.Startup)
		Return True
	End Function
End Module
